package org.example.design.designmode.prototype.javaClone;

import lombok.Data;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: songyafei
 * @Package: com.des.design.designMode.prototype
 * @ClassName: user
 * @Description: 用户类
 * @Param
 * @Exception
 * @CreateDate: 2020/5/11 21:24
 * @UpdateRemark: The modified content
 * @Version: 1.0
 * @Return :
 **/
public class User implements Cloneable,Serializable{

    private Integer id;

    private String idCard;

    private String name;

    private Integer age;

    private Long ismale;

    private List<String> userList;


    //通过java 自带的克隆类实现它重写克隆方法
    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    //使用字节流深拷贝
    public  User deepClone() throws IOException, ClassNotFoundException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream output = new ObjectOutputStream(bos);
        output.writeObject(this);

        ByteArrayInputStream inputStream = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream inputStream1 = new ObjectInputStream(inputStream);
        return (User) inputStream1.readObject();

    }
    //list 深拷贝
    public  User deepListClone() throws CloneNotSupportedException {
        User u = (User) super.clone();
        u.userList= (List) ((ArrayList)u.getUserList()).clone();
        return u;

    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Long getIsmale() {
        return ismale;
    }

    public void setIsmale(Long ismale) {
        this.ismale = ismale;
    }

    public List<String> getUserList() {
        return userList;
    }

    public void setUserList(List<String> userList) {
        this.userList = userList;
    }
}
